﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rename
{
    public partial class Form6 : Form
    {
        private ListView listView1;
        private int sortColumn = -1; // 当前排序列索引
        private SortOrder sortOrder = SortOrder.None; // 当前排序方向
        public Form6()
        {
            InitializeComponent();

            // 初始化 ListView
            listView1 = new ListView();
            listView1.Dock = DockStyle.Fill;
            listView1.View = View.Details;
            listView1.FullRowSelect = true;
            listView1.GridLines = true;
            listView1.AllowColumnReorder = true;

            // 添加列
            listView1.Columns.Add("ID", 80);
            listView1.Columns.Add("名称", 200);
            listView1.Columns.Add("数量", 100);
            listView1.Columns.Add("日期", 150);

            // 添加示例数据
            listView1.Items.Add(new ListViewItem(new[] { "3", "笔记本电脑", "15", "2023-05-10" }));
            listView1.Items.Add(new ListViewItem(new[] { "1", "鼠标", "50", "2023-03-15" }));
            listView1.Items.Add(new ListViewItem(new[] { "2", "键盘", "30", "2023-04-20" }));
            listView1.Items.Add(new ListViewItem(new[] { "5", "显示器", "8", "2023-06-05" }));
            listView1.Items.Add(new ListViewItem(new[] { "4", "耳机", "25", "2023-05-28" }));

            // 绑定列头点击事件
            listView1.ColumnClick += ListView1_ColumnClick;

            this.Controls.Add(listView1);

            this.Text = "支持列头排序的 ListView";
            this.Size = new Size(800, 500);
        }
        // 列头点击事件处理
        private void ListView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            // 如果点击的是当前排序列，则切换排序方向
            if (e.Column == sortColumn)
            {
                sortOrder = sortOrder == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
            }
            else
            {
                // 新列排序，默认升序
                sortColumn = e.Column;
                sortOrder = SortOrder.Ascending;
            }

            // 应用排序
            ApplySort();
        }
        // 应用排序
        private void ApplySort()
        {
            // 创建自定义比较器
            ListViewItemComparer comparer = new ListViewItemComparer(sortColumn, sortOrder);

            // 进行排序
            listView1.ListViewItemSorter = comparer;
            listView1.Sort();

            // 更新列头显示（添加排序指示）
            UpdateColumnHeaders();
        }

        // 更新列头显示，添加排序指示
        private void UpdateColumnHeaders()
        {
            // 重置所有列头文本
            for (int i = 0; i < listView1.Columns.Count; i++)
            {
                string originalText = listView1.Columns[i].Text.Trim(' ', '↑', '↓');
                listView1.Columns[i].Text = originalText;
            }

            // 为当前排序列添加指示符号
            if (sortColumn != -1 && sortOrder != SortOrder.None)
            {
                string sortIndicator = sortOrder == SortOrder.Ascending ? " ↑" : " ↓";
                listView1.Columns[sortColumn].Text += sortIndicator;
            }
        }

        // 自定义比较器类
        private class ListViewItemComparer : IComparer
        {
            private int column;
            private SortOrder order;

            public ListViewItemComparer(int column, SortOrder order)
            {
                this.column = column;
                this.order = order;
            }

            public int Compare(object x, object y)
            {
                ListViewItem itemX = x as ListViewItem;
                ListViewItem itemY = y as ListViewItem;

                if (itemX == null || itemY == null) return 0;

                string textX = itemX.SubItems[column].Text;
                string textY = itemY.SubItems[column].Text;

                // 尝试按数字比较
                if (int.TryParse(textX, out int numX) && int.TryParse(textY, out int numY))
                {
                    return CompareNumbers(numX, numY);
                }

                // 尝试按日期比较
                if (DateTime.TryParse(textX, out DateTime dateX) && DateTime.TryParse(textY, out DateTime dateY))
                {
                    return CompareDates(dateX, dateY);
                }

                // 默认为字符串比较
                return CompareStrings(textX, textY);
            }
            private int CompareNumbers(int x, int y)
            {
                return order == SortOrder.Ascending ? x.CompareTo(y) : y.CompareTo(x);
            }

            private int CompareDates(DateTime x, DateTime y)
            {
                return order == SortOrder.Ascending ? x.CompareTo(y) : y.CompareTo(x);
            }

            private int CompareStrings(string x, string y)
            {
                return order == SortOrder.Ascending
                    ? string.Compare(x, y)
                    : string.Compare(y, x);
            }
        }
    }
}
